;(function () {
    var time = Date.now();
    var index = {
        init: function () {
            this.load();
            this.bindScreenClick();
            this.bindMoreClick();
            this.bindSortClick();
            this.lazyBind();
            // 分页
            $("#pagination").whjPaging({
                //设为true时，ajax里的success函数必须调用setPage方法，否则分页插件显示保持不变
                isResetPage: true,
                callBack: function(currPage, pageSize) {
                    load(currPage, pageSize);
                }
            });
            // 滚动监听
            $(document).scroll(function (e) {
                if ($(document).scrollTop() > 500) {
                    $('.scroll-top').removeClass('hide');
                } else {
                    $('.scroll-top').addClass('hide');
                }
            });
            // 点击回到顶部
            $('.scroll-top').click(function () {
                $("html,body").animate({"scrollTop": 0})
            });
        },
        load: function () {
            var apartment = $('.active[data-apartment]').data('apartment');
            var area = $('.active[data-area]').data('area');
            var budget = $('.active[data-budget]').data('budget');
            var style = $('.active[data-style]').data('style');
            var sort = $('.active[data-sort]').data('sort');
            var _this = this;
            $.ajax({
                url: '/dd',
                data: {
                    apartment: apartment,
                    area: area,
                    budget: budget,
                    style: style,
                    sort: sort
                },
                dataType: 'json',
                type: 'POST',
                success: function (data) {
                    console.log(data);
                    //设置分页插件显示
                    $("#pagination").whjPaging("setPage", {
                        currPage: res.currPage,
                        totalPage: res.totalPage,
                        totalSize: res.totalSize,
                    });  
                    _this.lazyBind();
                },
                error: function (error) {
                    console.log(error);
                }
            });
        },
        lazyBind: function () {
            $("img.lazy").lazyload({
                effect : "fadeIn"
            });
            $("div.lazy").lazyload({
                effect : "fadeIn"
            });
            $("li.lazy").lazyload({
                effect : "fadeIn"
            });
        },
        // 点击筛选触发请求
        screen: function (newTime) {
            if (newTime - time >= 800) {
                time = Date.now();
            } else {
                return false;
            }
            this.load();
        },
        // 绑定筛选点击
        bindScreenClick: function () {
            var _this = this;
            $('.screen-list dl dd').click(function () {
                var index =$(this).index();
                $(this).addClass('active').siblings().removeClass('active');
                _this.screen(Date.now());
            });
        },
        // 绑定筛选的更多点击
        bindMoreClick: function () {
            $('.more').click(function () {
                var hasActive = $(this).hasClass('active');
                if (hasActive) {
                    $(this).removeClass('active');
                    $(this).parents('.screen-list').removeClass('heigh-auto');
                } else {
                    $(this).addClass('active');
                    $(this).parents('.screen-list').addClass('heigh-auto');
                }
            });
        },
        // 绑定排序点击
        bindSortClick: function () {
            var _this = this;
            $('.sort dd').click(function () {
                var hasActive = $(this).hasClass('active');
                var index =$(this).index();
                $(this).addClass('active').siblings().removeClass('active');
                _this.screen(Date.now());
            });
        }
    };
    index.init();
})()